<%inherit file="base.html"/>

<%def name="headIncludes()">
<link rel="stylesheet" href="${http_root}css/dataTables.bootstrap.min.css">
<link rel="stylesheet" href="${http_root}css/tautulli-dataTables.css">
<link rel="stylesheet" href="${http_root}css/dataTables.colVis.css">
<style>
    td {word-wrap: break-word}
</style>
</%def>

<%def name="headerIncludes()">
</%def>

<%def name="body()">
<div class='container-fluid'>
    <div class='table-card-header'>
        <div class="header-bar">
            <span id="sync-xml"><i class="fa fa-cloud-download"></i> Synced Items</span>
        </div>
        <div class="button-bar">
            % if _session['user_group'] == 'admin':
            <div class="btn-group">
                <button class="btn btn-danger btn-edit" data-toggle="button" aria-pressed="false" autocomplete="off" id="sync-row-edit-mode">
                    <i class="fa fa-trash-o"></i> Delete mode
                </button>
            </div>
            % endif
            % if _session['user_group'] == 'admin':
            <div class="btn-group" id="user-selection">
                <label>
                    <select name="sync-user" id="sync-user" class="btn" style="color: inherit;">
                        <option value="">All Users</option>
                        <option disabled>&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;</option>
                    </select>
                </label>
            </div>
            % endif
            <div class="btn-group">
                <button class="btn btn-dark refresh-syncs-button" id="refresh-syncs-list"><i class="fa fa-refresh"></i> Refresh synced items</button>
            </div>
            <div class="btn-group colvis-button-bar"></div>
        </div>
    </div>
    <div class='table-card-back'>
        <table class="display sync_table" id="sync_table" width="100%">
            <thead>
            <tr>
                <th align="left" id="delete_row">Delete</th>
                <th align="left" id="state">State</th>
                <th align="left" id="user">User</th>
                <th align="left" id="sync_title">Title</th>
                <th align="left" id="type">Type</th>
                <th align="left" id="platform">Platform</th>
                <th align="left" id="device">Device</th>
                <th align="left" id="size">Total Size</th>
                <th align="left" id="items">Total Items</th>
                <th align="left" id="converted">Converted</th>
                <th align="left" id="downloaded">Downloaded</th>
                <th align="left" id="percent_complete">Complete</th>
            </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
</div>
</%def>

<%def name="modalIncludes()">
<div class="modal fade" id="confirm-modal-delete" tabindex="-1" role="dialog" aria-labelledby="confirm-modal-delete">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="fa fa-remove"></i></button>
                <h4 class="modal-title" id="myModalLabel">Confirm Delete</h4>
            </div>
            <div class="modal-body" style="text-align: center;">
                <p>Are you REALLY sure you want to delete <strong><span id="deleteCount"></span></strong> sync item(s)?</p>
                <p>This is permanent and cannot be undone!</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-dark" data-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-danger btn-ok" data-dismiss="modal" id="confirm-delete">Delete</button>
            </div>
        </div>
    </div>
</div>
</%def>

<%def name="javascriptIncludes()">
<script src="${http_root}js/jquery.dataTables.min.js"></script>
<script src="${http_root}js/dataTables.bootstrap.min.js"></script>
<script src="${http_root}js/dataTables.bootstrap.pagination.js"></script>
<script src="${http_root}js/dataTables.colVis.js"></script>
<script src="${http_root}js/tables/sync_table.js${cache_param}"></script>
<script>
    $(document).ready(function() {
        // Load user ids and names (for the selector)
        $.ajax({
            url: 'get_user_names',
            type: 'GET',
            dataType: 'json',
            success: function (data) {
                var select = $('#sync-user');
                data.sort(function (a, b) {
                    return a.friendly_name.localeCompare(b.friendly_name);
                });
                data.forEach(function (item) {
                    select.append('<option value="' + item.user_id + '">' +
                        item.friendly_name + '</option>');
                });
            }
        });

        function loadSyncTable(selected_user_id) {
            sync_table_options.ajax = {
                url: 'get_sync?user_id=' + selected_user_id,
                type: 'POST'
            };
            sync_table = $('#sync_table').DataTable(sync_table_options);
            var colvis = new $.fn.dataTable.ColVis(sync_table, {
                buttonText: '<i class="fa fa-columns"></i> Select columns',
                buttonClass: 'btn btn-dark',
                exclude: [0]
            });
            $(colvis.button()).appendTo('div.colvis-button-bar');

            clearSearchButton('sync_table', sync_table);

            $('#sync-user').on('change', function () {
                selected_user_id = $(this).val() || null;
                sync_table.ajax.url('get_sync?user_id=' + selected_user_id).load();
            });
        }

        var selected_user_id = "${_session['user_group']}" == "admin" ? null : "${_session['user_id']}";
        loadSyncTable(selected_user_id);

        % if _session['user_group'] == 'admin':
        $('#sync-row-edit-mode').on('click', function() {
            if ($(this).hasClass('active')) {
                $(this).tooltip('destroy');

                if (syncs_to_delete.length > 0) {
                    $('#deleteCount').text(syncs_to_delete.length);
                    $('#confirm-modal-delete').modal();
                    $('#confirm-modal-delete').one('click', '#confirm-delete', function () {
                        syncs_to_delete.forEach(function(row, idx) {
                            $.ajax({
                                url: 'delete_sync_rows',
                                type: 'POST',
                                data: {
                                    client_id: row.client_id,
                                    sync_id: row.sync_id
                                },
                                async: true,
                                success: function (data) {
                                    var msg = "Sync deleted";
                                    showMsg(msg, false, true, 2000);
                                }
                            });
                        });
                        sync_table.ajax.reload();
                    });
                }

                $('.delete-control').each(function () {
                    $(this).addClass('hidden');
                    $('#sync-row-edit-mode-alert').fadeOut(200);
                });

            } else {
                $(this).tooltip({
                    container: '.body-container',
                    placement: 'bottom',
                    title: 'Select syncs to delete. Data is deleted upon exiting delete mode.',
                    trigger: 'manual'
                }).tooltip('show');

                syncs_to_delete = [];
                $('.delete-control').each(function() {
                    $(this).find('button.btn-danger').toggleClass('btn-warning').toggleClass('btn-danger');
                    $(this).removeClass('hidden');
                });
            }
        });
        % endif
    });

    $("#refresh-syncs-list").click(function() {
        sync_table.ajax.reload();
    });

    $('#sync-xml').on('tripleclick', function () {
        openPlexXML('/servers/{machine_id}/sync_lists', true);
    });
</script>
</%def>
